<template>
  <div>
    <Form class="custom-ivu-form mr24 text-right" inline @submit.native.prevent="handleFormSubmit">
      <FormItem style="margin-bottom: 0">
        <Input
          v-model="formData.k"
          :label-width="60"
          placeholder="请输入搜索内容"
          class="form-item--width"
          size="large"
          clearable
        ></Input>
      </FormItem>

      <Button type="primary" html-type="submit" style="width: 87px">查询</Button>
      <Button type="primary" style="width: 87px" class="ml14" @click="visible.create = true">新增</Button>
    </Form>

    <div class="list-table-box relative mt20">
      <CommonTablePage
        :loading="tableLoading"
        :columns="columns"
        :tableData="tableData"
        :current="formData.pageNo"
        :total="pagination.total"
        @on-change="handlePageChange"
        @on-page-size-change="handlePageSizeChange"
      ></CommonTablePage>
    </div>

    <ProvideUnitCreate :visible.sync="visible.create" @on-add-success="handleFormSubmit"></ProvideUnitCreate>
  </div>
</template>

<script>
// components
import CommonTablePage from '@/components/common/CommonTablePage/CommonTablePage'
import ProvideUnitCreate from './Modal/ProvideUnitCreate.vue'

// util
import { createCommonPagination } from '@/utils/service.js'
import { handleRequestStatus } from '@/utils/uitls.js'
import { createForm, createColumns } from './service/provideUnit.js'

// request
import { getSystemOrganList, systemOrganUpdate, systemOrganRemove } from '@/api/system/organConfig.js'
export default {
  name: 'ProvideUnitConfig',

  components: { CommonTablePage, ProvideUnitCreate },

  data() {
    return {
      formData: createForm(),

      tableLoading: false,
      columns: createColumns(this),
      tableData: [],

      pagination: createCommonPagination(123),

      visible: {
        create: false,
      },
    }
  },

  methods: {
    handleEditTableRow(row) {
      const params = {
        name: row.organName_bk,
        produceNum: Number(row.produceNum_bk),
      }
      // console.log('params', params)
      this.systemOrganUpdate(row.organId, params)
    },

    handleEditTableRowCancel(row) {
      row.organName_bk = row.organName
      row.produceNum_bk = row.produceNum
    },

    handleDeleteTableRow(row) {
      // console.log('handleDeleteTableRow', row)
      this.systemOrganRemove(row.organId)
    },

    handleFormSubmit(no_reset) {
      if (no_reset !== true) {
        this.formData.pageNo = 1
      }
      this.getSystemOrganList({ ...this.formData })
    },

    handlePageChange(page) {
      this.formData.pageNo = page
      this.handleFormSubmit(true)
    },
    handlePageSizeChange(pageSize) {
      this.formData.pageSize = pageSize
      this.handleFormSubmit(true)
    },

    async getSystemOrganList(params) {
      this.tableLoading = true
      const res = await getSystemOrganList(params)
      // console.log('getSystemOrganList', res)
      const data = res.data || {}
      if (data.status !== 0) {
        this.tableLoading = false
        this.tableData = []
        this.pagination.total = 0
        return
      }
      this.tableData = (data.data.data || []).map((it) => ({
        ...it,
        organName_bk: it.organName || '',
        produceNum_bk: it.produceNum || 0,
        editable: false,
      }))
      this.pagination.total = data.data.count
      this.tableLoading = false
    },

    async systemOrganUpdate(id, params) {
      const res = await systemOrganUpdate(id, params)
      handleRequestStatus(res, '编辑', this.handleFormSubmit)
    },

    async systemOrganRemove(id) {
      const res = await systemOrganRemove(id)
      handleRequestStatus(res, '删除', this.handleFormSubmit)
    },
  },

  mounted() {
    this.handleFormSubmit()
  },
}
</script>

<style lang="less" scoped></style>
